package com.servlet;

import com.beans.AdminInfo;
import com.beans.MenuInfo;
import com.beans.PageInfo;
import com.beans.RoleInfo;
import com.dao.AdminDao;
import com.dao.MenuDao;
import com.dao.RoleDao;
import com.utils.PageUtil;
import com.utils.StrUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/RoleServlet.do")
public class RoleServlet extends HttpServlet {
    RoleDao roleDao=new RoleDao();
    AdminDao adminDao=new AdminDao();
    MenuDao menuDao=new MenuDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag=request.getParameter("flag");
        if("manage".equals(flag)){
            manage(request,response);
        }
        else if("searchForUpdate".equals(flag)){
            searchForUpdate(request,response);
        }
        else if("updateRole".equals(flag)){
            updateRole(request,response);
        }
        else if("delete".equals(flag)){
            delete(request,response);
        }
        else if("updateAdminRole".equals(flag)){
            updateAdminRole(request,response);
        }
        else if("listAdmin".equals(flag)){
            ListAdmin(request,response);
        }
        else if("adminRole".equals(flag)){
            adminRole(request,response);
        }
        else if("roleMenu".equals(flag)){
            roleMenu(request,response);
        }
        else if("add".equals(flag)){
            add(request,response);
        }
        else if("validate".equals(flag)){
            validate(request,response);
        }
        else if("updateRoleMenu".equals(flag)){
            updateRoleMenu(request,response);
        }
        else if("delRole".equals(flag)){
            delRole(request,response);
        }
    }

    private void delRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] ids = request.getParameterValues("ck_id");
        roleDao.delRole(ids);
        request.setAttribute("adminManage", "删除成功");
        manage(request, response);
    }

    //更新角色权限
    private void updateRoleMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer roleId=Integer.parseInt(request.getParameter("roleId"));
        String []  menuIds =request.getParameterValues("ids");

        roleDao.updateRoleMenu(roleId,menuIds);
        request.setAttribute("msg", "角色信息更新成功");

        roleMenu(request,response);
    }
    //验证角色重复
    private void validate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String roleName = request.getParameter("roleName");
        RoleInfo role = roleDao.getSingleRole(roleName);
        if (role != null) {
            response.getWriter().print("-1");
        }
    }

    //添加角色
    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roleName = request.getParameter("roleName");
        String des = request.getParameter("des");

        RoleInfo role = new RoleInfo();
        role.setRoleName(roleName);
        role.setDes(des);

        int result = roleDao.addRole(role);
        if(result==1) {
            request.setAttribute("msg", "角色创建成功");
            request.getRequestDispatcher("/role/role_add.jsp").forward(request, response);
        }
    }

    //查询角色信息
    private void roleMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id=Integer.parseInt(request.getParameter("roleId"));
        RoleInfo roleInfo=roleDao.getRoleById(id);
        List<MenuInfo> menuList = menuDao.getMenuList(0);
        List<String> menuIds= roleDao.getMenuIds(id);
        request.setAttribute("menuIds",menuIds);
        request.setAttribute("roleInfo",roleInfo);
        request.setAttribute("menuList",menuList);
        request.getRequestDispatcher("/role/rolemenu_edit.jsp").forward(request,response);
    }

    //转向用户角色分配表
    private void adminRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id=Integer.parseInt(request.getParameter("id"));
        AdminInfo admin=adminDao.getAdminById(id);
        List<RoleInfo> roleList=roleDao.getAllRole();

        if(roleList!=null){
            request.setAttribute("admin",admin);
            request.setAttribute("roleList",roleList);
            request.getRequestDispatcher("/role/admin_role.jsp").forward(request,response);
        }

    }
    //查询出所有的用户角色
    private void ListAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageIndex = 1;
        int pageSize = 2;
        String pageIndexStr = request.getParameter("pageIndex");
        int rowCount=adminDao.getAdminCount();
        if (!StrUtil.isNullOrEmpty(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
        List<AdminInfo> adminList= adminDao.getAllAdmin(page);
        if(adminList!=null){
            request.setAttribute("adminList",adminList);
            request.setAttribute("pageInfo",page);
            request.getRequestDispatcher("/role/admin_list.jsp").forward(request, response);
        }

    }
    //更新用户角色
    private void updateAdminRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer adminId=Integer.parseInt(request.getParameter("id"));
        Integer roleId=Integer.parseInt(request.getParameter("roleId"));
        adminDao.updateAdminRole(adminId,roleId);
        request.setAttribute("msg","角色更新成功");
        adminRole(request,response);
    }

    //删除角色
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id=Integer.parseInt(request.getParameter("id"));
        int result=roleDao.deleteRoleById(id);
        if(result==1){
         request.setAttribute("msg","角色删除成功");
         manage(request,response);
        }
    }
    //角色更新
    private void updateRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id= Integer.parseInt(request.getParameter("id"));
        String roleName=request.getParameter("roleName");
        String des=request.getParameter("des");
        RoleInfo role=new RoleInfo();
        role.setId(id);
        role.setRoleName(roleName);
        role.setDes(des);
        int result=roleDao.updateRole(role);
        if(result==1){
            request.setAttribute("role",role);
            request.setAttribute("msg","角色修改成功");
            request.getRequestDispatcher("/role/update_role.jsp").forward(request,response);
        }
    }
    //转向角色更新界面
    private void searchForUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id=Integer.parseInt(request.getParameter("id"));
        RoleInfo role=roleDao.getRoleById(id);
        if(role!=null){
            request.setAttribute("role",role);
            request.getRequestDispatcher("/role/update_role.jsp").forward(request,response);
        }
    }
    //角色管理
    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageIndex = 1;
        int pageSize = 2;
        String pageIndexStr = request.getParameter("pageIndex");
        int rowCount = roleDao.getRoleCount();
        if (!StrUtil.isNullOrEmpty(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
        List<RoleInfo> roleList = roleDao.getRoleList(page);
        if(roleList!=null){
            request.setAttribute("roleList", roleList);
            request.setAttribute("pageInfo", page);
            request.getRequestDispatcher("/role/role_manage.jsp").forward(request, response);
        }

    }
}
